package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.dialog.ECProgressDialog;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 提现确认
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/21
 */

public class WithdrawConfirmActivity extends BaseChatActivity implements ICommonViewUi {

    public static final String PAY_TYPE = "pay_type";
    public static final String ALIPAY = "alipay";
    public static final String WECHAT = "wechat";
    public static final String PAYPAL = "paypal";
    private String payType="";
    @InjectView(R.id.account_edt)
    EditText accountEdt;
    @InjectView(R.id.coins_edt)
    EditText coinsEdt;
    @InjectView(R.id.confirm_btn)
    Button confirmBtn;

    public static final String PAY_COINS="pay_coins";
    private String coins="";

    private ICommonRequestPresenter iCommonRequestPresenter;

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_withdraw_confirm;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Withdraw");
        iCommonRequestPresenter = new CommonRequestPresenterImpl(mContext, this);
        payType=getIntent().getStringExtra(PAY_TYPE);
        switch (payType){
            case ALIPAY:
                payType="支付宝";
                accountEdt.setHint("Please enter your Ali account");
                break;
            case WECHAT:
                payType="微信支付";
                accountEdt.setHint("Please enter your Wechat account");
                break;
            case PAYPAL:
                payType="PayPal";
                accountEdt.setHint("Please enter your Paypal account");
                break;
        }
        coins=getIntent().getStringExtra(PAY_COINS);
    }

    @OnClick(R.id.confirm_btn)
    public void onConfirmBtnClick(){
        if (TextUtils.isEmpty(accountEdt.getText().toString().trim())){
            showToastLong("Please enter your account");
            return;
        }

        if (TextUtils.isEmpty(coinsEdt.getText().toString().trim())){
            showToastLong("Please enter conis number");
            return;
        }

        if (Integer.parseInt(coinsEdt.getText().toString().toString())<100){
            showToastLong("At least 100");
            return;
        }
        if (Double.parseDouble(coinsEdt.getText().toString().toString())>Double.parseDouble(coins)){
            showToastLong("It's not enough,you just have "+coins);
            return;
        }
        toRequest(ApiConstants.EventTags.BEGIN_EVENT);

    }

    @Override
    public void toRequest(int eventTag) {
        RequestParams map = new RequestParams();
        map.put("channel",payType);
        map.put("channelAccount",accountEdt.getText().toString().trim());
        map.put("amount",coinsEdt.getText().toString().trim());
        iCommonRequestPresenter.request(ApiConstants.EventTags.BEGIN_EVENT, mContext, ApiConstants.Urls.ACCOUNT_TAKE_OUT, map);
    }

    @Override
    public void getRequestData(int eventTag, String result) {
//        showToastLong(HttpStatusUtil.getStatusMsg(result));
        PostResult postResult = new PostResult();
        postResult.setTag(EventBusTags.ACCOUNT_TAKE_OUT);
        EventBus.getDefault().post(postResult);
        finish();
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
        showToastLong(msg);
    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {
        showToastLong(msg);
    }
    private ECProgressDialog mPostingdialog;
    @Override
    public void isRequesting(int eventTag, boolean status) {
        if (status) {
            mPostingdialog = new ECProgressDialog(this, "Waiting");
            mPostingdialog.show();
        } else {
            mPostingdialog.dismiss();
        }
    }
}
